#include "timer.h"

Uint32 Timer::_TimerProc(Uint32 interval, void* param)
{
	//cast param
	Timer* pTimer=(Timer*)param;

	//call timer handler
	pTimer->OnTimer();

	//return new interval
	return(pTimer->GetInterval());
}

Timer::Timer(int interval)
{
	//set the interval
	SetInterval(interval);
}

//destructor
Timer::~Timer()
{
	//stop the timer
	Stop();
}

//get interval
int Timer::GetInterval()
{
	//return the current interval
	return(_interval);
}

//set interval
void Timer::SetInterval(int Interval)
{
	//set new interval
	_interval=Interval;
}

//get timer id
SDL_TimerID Timer::GetTimerID()
{
	//return the timer id
	return(_timerID);
}

//start timer
void Timer::Start(FuncVoid_Callback func)
{
	_func = func;

	//stop timer if already exists
	if(GetTimerID())
		Stop();

	//start the timer
	_timerID=SDL_AddTimer((Uint32)GetInterval(),Timer::_TimerProc,this);
}

//stop timer
void Timer::Stop()
{
	//stop timer if it exists
	if(GetTimerID())
	{
		//kill the timer
		SDL_RemoveTimer(GetTimerID());
		//set timerid to 0
		_timerID=0;
	}
}

//on timer handler
void Timer::OnTimer()
{
	_func();	
}